﻿using System;
using PirateMap.Models.cls;
using System.Web.Http;

namespace PirateMap.Controllers.api
{
  public class PirateMapController : ApiController
  {
    // GET api/piratemap?name=Dimko
    [HttpGet]
    public string Get([FromUri]string name)
    {
      try
      {
        if (!Game.CheckPlayer(name))
        {
          Game.AddNewPlayer(new Player(name));
        }

        return Game.GetPlayerByName(name).Id;
      }
      catch
      {
        return "error";
      }
    }

    // GET api/piratemap?id=lhasgflhaghlhfglfdl&direction=up
    [HttpGet]
    public string Get([FromUri]string id, [FromUri]string direction)
    {
      try
      {
        switch (direction.ToLower())
        {
          case "u":
          case "up":
            return Game.Move(id, DirectionTag.Up);
          case "d":
          case "down":
            return Game.Move(id, DirectionTag.Down);
          case "l":
          case "left":
            return Game.Move(id, DirectionTag.Left);
          case "r":
          case "right":
            return Game.Move(id, DirectionTag.Right);
          default:
            return "error";
        }
      }
      catch(Exception e)
      {
        return e.Message;
      }
    }

    // POST api/piratemap
    //public void Post([FromBody]string value)
    //{
    //}
  }
}
